package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.domain.ShBannerPicture;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.ShBanner;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IShBannerService;
import com.ruoyi.system.utils.TencentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShBannerServiceImpl implements IShBannerService {
    @Autowired
    private ShBannerMapper shBannerMapper;
    @Autowired
    private ShBannerPictureMapper shBannerPictureMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public ShBanner selectShBannerById(Long id) {
        return shBannerMapper.selectShBannerById(id);
    }

    @Override
    public List<ShBanner> selectShBannerList(ShBanner shBanner) {
        List<ShBanner> list = shBannerMapper.selectShBannerList(shBanner);
        for (ShBanner obj : list) {
            SysUser sysUser = sysUserMapper.selectUserById(obj.getUserId());
            obj.setUserName(sysUser.getUserName());
            ShBannerPicture paramPic = new ShBannerPicture();
            paramPic.setBannerId(obj.getId());
            paramPic.setIsVideo(0);
            List<ShBannerPicture> picList = shBannerPictureMapper.selectShBannerPictureList(paramPic);
            obj.setPicList(picList);
            if (null != picList && picList.size() > 0) {
                obj.setPicId(picList.get(0).getId());
                obj.setFileFj(picList.get(0).getProductPic());
            }
            if (null!=shBanner&&null!=shBanner.getIsVideo()&&shBanner.getIsVideo()==1) {
                ShBannerPicture paramVid = new ShBannerPicture();
                paramVid.setBannerId(obj.getId());
                paramVid.setIsVideo(1);
                List<ShBannerPicture> vidList = shBannerPictureMapper.selectShBannerPictureList(paramPic);
                obj.setVidList(vidList);
                if (null != vidList && vidList.size() > 0) {
                    obj.setVidId(vidList.get(0).getId());
                    obj.setVidUrl(vidList.get(0).getProductPic());
                }
            }
        }
        return list;
    }

    @Override
    public int insertShBanner(ShBanner shBanner) {
        shBanner.setCreateTime(DateUtils.getNowDate());
        redisService.delCacheMap("BANNER:DP");
        return shBannerMapper.insertShBanner(shBanner);
    }

    @Override
    public int updateShBanner(ShBanner shBanner) {
        redisService.delCacheMap("BANNER:DP");
        return shBannerMapper.updateShBanner(shBanner);
    }

    @Override
    public int deleteShBannerByIds(Long[] ids) {
        redisService.delCacheMap("BANNER:DP");
        TencentUtils tencentUtils = TencentUtils.getInstance();
        for (Long id:ids) {
            ShBannerPicture paramPic = new ShBannerPicture();
            paramPic.setBannerId(id);
            List<ShBannerPicture> picList = shBannerPictureMapper.selectShBannerPictureList(paramPic);
            for (ShBannerPicture pic:picList) {
                if (null != pic.getProductPic() && pic.getProductPic().indexOf("aliyuncs.com/") > 0) {
                    String[] keyAry = pic.getProductPic().split("aliyuncs.com/");
                    tencentUtils.del(keyAry[1]);
                }
                shBannerPictureMapper.deleteShBannerPictureById(id);
            }
        }
        return shBannerMapper.deleteShBannerByIds(ids);
    }

    @Override
    public int deleteShBannerById(Long id) {
        redisService.delCacheMap("BANNER:DP");
        TencentUtils tencentUtils = TencentUtils.getInstance();
        ShBannerPicture paramPic = new ShBannerPicture();
        paramPic.setBannerId(id);
        List<ShBannerPicture> picList = shBannerPictureMapper.selectShBannerPictureList(paramPic);
        for (ShBannerPicture pic:picList) {
            if (null != pic.getProductPic() && pic.getProductPic().indexOf("aliyuncs.com/") > 0) {
                String[] keyAry = pic.getProductPic().split("aliyuncs.com/");
                tencentUtils.del(keyAry[1]);
            }
            shBannerPictureMapper.deleteShBannerPictureById(id);
        }
        return shBannerMapper.deleteShBannerById(id);
    }
}
